package com.zhouyibike.uu.ui.activity.web;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.zhouyibike.uu.R;
import com.zhouyibike.uu.ui.base.BaseActivity;

public class TotalWebActivity extends BaseActivity implements View.OnClickListener {
    private ImageButton mBtnBack;
    private TextView mBtnTitle;
    private WebView wv_show;
    private String title, url;
    private int fontSize = 3;
    ProgressBar bar ;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_total_web);
        title = getIntent().getStringExtra("title");
        url = getIntent().getStringExtra("url");
        bar = (ProgressBar)findViewById(R.id.myProgressBar);
        initView();
        showProgressDialog();
        initWV();
    }

    private void initView() {
        mBtnBack = (ImageButton) findViewById(R.id.bar_btn_back);
        mBtnTitle = (TextView) findViewById(R.id.bar_text_title);
        mBtnTitle.setText(title);
        mBtnBack.setOnClickListener(this);
        wv_show = (WebView) findViewById(R.id.wv_totalweb_show);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bar_btn_back:
                finish();
                break;
        }
    }

    private void initWV() {
        //设置支持JavaScript脚本
        WebSettings webSettings = wv_show.getSettings();
        webSettings.setJavaScriptEnabled(true);
        //设置可以访问文件
        webSettings.setAllowFileAccess(true);
        //设置支持缩放
        webSettings.setBuiltInZoomControls(false);

        webSettings.setDatabaseEnabled(true);

        //使用localStorage则必须打开
        webSettings.setDomStorageEnabled(true);

        webSettings.setGeolocationEnabled(true);
        switch (fontSize) {

            case 1:
                webSettings.setTextSize(WebSettings.TextSize.SMALLEST);
                break;
            case 2:
                webSettings.setTextSize(WebSettings.TextSize.SMALLER);
                break;
            case 3:
                webSettings.setTextSize(WebSettings.TextSize.NORMAL);
                break;
            case 4:
                webSettings.setTextSize(WebSettings.TextSize.LARGER);
                break;
            case 5:
                webSettings.setTextSize(WebSettings.TextSize.LARGEST);
                break;
        }

//        wv_show.setWebChromeClient(new WebChromeClient() {
//
//            @Override
//            //设置网页加载的进度条
//            public void onProgressChanged(WebView view, int newProgress) {
//                if (newProgress >= 100) {
//                    dismissProgressDialog();
//                }
//                super.onProgressChanged(view, newProgress);
//            }
//        });
        //
        wv_show.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                //设定加载结束的操作
                dismissProgressDialog();
            }
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

//        wv_show.setWebChromeClient(new WebChromeClient() {
//
//            @Override
//            public void onProgressChanged(WebView view, int newProgress) {
//                if (newProgress == 100) {
//                    bar.setVisibility(View.INVISIBLE);
//                    wv_show.setVisibility(View.VISIBLE);
//                } else {
//                    wv_show.setVisibility(View.INVISIBLE);
//                    if (View.INVISIBLE == bar.getVisibility()) {
//                        bar.setVisibility(View.VISIBLE);
//                    }
//                    bar.setProgress(newProgress);
//                }
//                super.onProgressChanged(view, newProgress);
//            }
//        });
        wv_show.loadUrl(url);
    }
}
